import os
import re

_DOA3="""
.subckt {my} 1OUT 1IN- 1IN+
{lib}
XU1 1OUT 1IN- 1IN+ 0 0 {model}
.ends {my}
"""

_DOA8="""
.subckt {my} 1OUT 1IN- 1IN+ VEE 2IN+ 2IN- 2OUT VCC
{lib}
XU1 1OUT 1IN- 1IN+ VEE VCC {model}
XU2 2OUT 2IN- 2IN+ VEE VCC {model}
.ends {my}
"""

# 生成空的子电路
def _nullModel(name, pin:int):
    return f'null_pin{pin}', f'.subckt null_pin{pin} {" ".join(map(str,range(1,pin+1)))}\n.ends null_pin{pin}', f'{name} 视为{pin}pin空的子电路'

# 子电路名称需和文件名保持一致
def _getU(name:str, pin_num, model_name:str, default_name:str):
    waring = ''
    path = f'libs/{model_name}.lib'
    if not os.path.exists(path):
        waring = f'{name} 没有找到对应的模型文件-{model_name}, 默认使用模型-{default_name}'
        model_name = default_name
        path = f"libs/{default_name}.lib"
    lib = f'.include "{path}"'
    pass # 直接使用了默认模型
    # with open(path, mode='r', encoding='utf-8') as file:
    #     # 端口数检测
    #     for row in file.readlines():
    #         if row.lower().startswith(f'.subckt {model_name.lower()}'):
    #             p = len(row.strip().split())-2
    #             break
    # if p is None: 
    #     waring = f'{waring and (waring+"\n")}{model_name}未成功解析端口数量, 默认视为5端口放大器元件'
    #     p = 5
    if pin_num in (3,8):
        model_name0=model_name
        model_name +=str(pin_num)
        lib = (_DOA8 if pin_num==8 else _DOA3).format(my=model_name,model=model_name0, lib=lib)
    return model_name, lib, waring

def getModel(name:str, cls_:str, value:str, ignore, pin_num)->tuple[str, str]|tuple[str, str, str]|None:
     # 接口
    if ignore:
        # 返回空子电路
        return _nullModel(name, pin_num)
    # 二极管
    if cls_=='D':
        if value.startswith('1N4148'):
            return 'D1N4148', '.MODEL D1N4148 D (IS=2.52n N=1.752 RS=0.575 XTI=3 EG=1.11)'
        elif value.startswith('1N4001'):
            return 'D1N4001', '.MODEL D1N4001 D (IS=7.5p N=1.8 RS=0.1 XTI=3 EG=1.11)'
        elif value.startswith('MMBD4148SE'):
            return 'MMBD4148SE', '.MODEL MMBD4148SE D (IS=1.81E-15 N=1.072 RS=0.557 BV=100 IBV=100U)'
        elif value.startswith('1N5819'):
            return 'D1N4001', '.MODEL D1N5819 D (IS=5u N=1.05 RS=0.15 BV=40 IBV=1u)'
        elif value.startswith('1N4733A'):
            return 'D1N4733A', '.MODEL D1N4733A D (IS=1e-14 N=2 BV=5.1 IBV=1e-3)'
        elif value.startswith('LTL'):
            return 'LED_RED', '.MODEL LED_RED D (IS=1e-18 N=1.8 RS=10 BV=5 IBV=1e-3)'
        elif value.startswith('1SDA10'):
            return 'BR1SDA10', '.MODEL BR1SDA10 D (IS=1e-12 N=1.7 RS=0.2)'
        elif value.startswith('P6KE6'):
            return 'P6KE6_8A', '.MODEL P6KE6_8A D (IS=1e-14 N=2 BV=6.8 IBV=1e-3 CJO=50p)'
        else:
            return 'D1N4148', '.MODEL D1N4148 D (IS=2.52n N=1.752 RS=0.575 XTI=3 EG=1.11)', f'{name} 没有找到型号 {value}, 默认使用 D1N4148'
    # 放大器
    elif cls_ == 'U':
        for key in ('LM833', 'SGM5532', 'SGM8262', 'TP358'):
            if value.startswith(key):
                return _getU(name,pin_num, key, 'OPAMP')
        return _getU(name,pin_num, value, 'OPAMP')
    return None